﻿using DB.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi_For_Auth.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserDetailController : ControllerBase
    {
        // GET: api/<UserDetailController>
        [HttpGet]
        public List<UserDetail> Get()
        {
            using(DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.UserDetails
                    .Include(o => o.RegistUser)
                        .ThenInclude(o => o.Role)
                            .ThenInclude(o => o.RoleComparePageControls)
                    .Include(o => o.RegistUser)
                        .ThenInclude(o => o.Department)
                    .ToList();
                return data;
            }
        }

        // GET api/<UserDetailController>/5
        [HttpGet("{id}")]
        public UserDetail Get(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var data = db.UserDetails
                    .Where(o => o.Id == id)
                    .Include(o => o.RegistUser)
                        .ThenInclude(o => o.Role)
                            .ThenInclude(o => o.RoleComparePageControls)
                    .Include(o => o.RegistUser)
                        .ThenInclude(o => o.Department)
                    .SingleOrDefault();
                return data;
            }
        }

        // POST api/<UserDetailController>
        [HttpPost]
        public void Post([FromBody] UserDetail value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                db.UserDetails.Add(value);
                db.SaveChanges();
            }
        }

        // PUT api/<UserDetailController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] UserDetail value)
        {
            if (value.Id == null)
            {
                value.Id = Guid.NewGuid().ToString();
            }
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var userDetail = db.UserDetails.Where(o => o.Id == value.Id).SingleOrDefault();
                if (userDetail != null)
                {
                    userDetail.Key = value.Key;
                    userDetail.Value = value.Value;
                    userDetail.UpdateTime = value.UpdateTime;
                    // Update other properties as needed
                    db.SaveChanges();
                }
            }
        }

        // DELETE api/<UserDetailController>/5
        [HttpDelete("{id}")]
        public void Delete(string id)
        {
            using (DB.Models.QiAuthBaseContext db = new DB.Models.QiAuthBaseContext())
            {
                var userDetail = db.UserDetails.Where(o => o.Id == id).SingleOrDefault();
                if (userDetail != null)
                {
                    db.UserDetails.Remove(userDetail);
                    db.SaveChanges();
                }
            }
        }
    }
}
